package com.snack.common.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@SuppressWarnings("unchecked")
public class ObjectFactory {
	private Class<?> claxx;

	public ObjectFactory(Class<?> clazz) {
		this.claxx = clazz;
	}
	
	public ObjectFactory(String className) throws ClassNotFoundException{
		this.claxx = ClassLoaderUtils.loadClass(className, ObjectFactory.class);
	}

	public <T> T newInstance( ) throws InstantiationException, IllegalAccessException {
		T obj = (T) this.claxx.newInstance();
		return obj;
	}
	
	public <T> T newInstance(Class<?>[] argsClasses, Object[] constructorArgs) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException  {     
                                                                                                                                              
	    Constructor<?>  cons = this.claxx.getConstructor(argsClasses);                   
	                                                                                  
	    return (T)cons.newInstance(constructorArgs);                                                
	                                                                                  
	}                 

	public static void bindData(Object target, Map<String, Object> propertyValues) {
		ObjectUtils.setProperties(target, propertyValues);
	}

	public static void bindData(Object target, String name, Object value) {
		ObjectUtils.setProperty(target, name, value);
	}
	
}
